package com.example.lenovo.permissions4m_lxy;

import android.Manifest;
import android.support.annotation.NonNull;
import android.view.View;

import com.example.lenovo.permissions4m_lxy.common.BaseActivity;
import com.example.lenovo.permissions4m_lxy.utils.ToastUtil;
import com.example.lenovo.permissions4m_lxy.utils.permission.PermissionCallback;
import com.example.lenovo.permissions4m_lxy.utils.permission.Permissionhelp;
import com.joker.api.Permissions4M;

/**
 * Created by lenovo on 2017/10/19.
 */

public class AdvancedActivity extends BaseActivity implements View.OnClickListener {


    private static final int CALL_LOG_CODE = 1;// 查询通过记录
    private static final int READ_CONTACTS_CODE = 2;// 查询通讯录
    private View mBtn_calllog;
    private View mBtn_readcontacts;

    @Override
    public int getLayoutId() {
        return R.layout.activity_advanced;
    }

    @Override
    protected void initView() {
        mBtn_calllog = obtainView(R.id.btn_calllog);
        mBtn_readcontacts = obtainView(R.id.btn_readcontacts);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setListener() {
        mBtn_calllog.setOnClickListener(this);
        mBtn_readcontacts.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            // 读取通话记录权限成功
            case R.id.btn_calllog:
                Permissionhelp.getPermission(mActivity, Manifest.permission.READ_CALL_LOG, CALL_LOG_CODE, new PermissionCallback() {
                    @Override
                    public void onSuccessful() {
                        ToastUtil.show("读取通话记录权限成功 封装成功");
                    }
                });
                // 这个是非注解
//                Permissions4M.get(mActivity)
//                        .requestPermissions(Manifest.permission.READ_CALL_LOG)
//                        .requestCodes(CALL_LOG_CODE)
//                        .requestPageType(Permissions4M.PageType.MANAGER_PAGE)
//                        .request();
                break;
            // 读取通讯录权限成功
            case R.id.btn_readcontacts:
                Permissionhelp.getPermission(mActivity, Manifest.permission.READ_CONTACTS, READ_CONTACTS_CODE, new PermissionCallback() {
                    @Override
                    public void onSuccessful() {
                        ToastUtil.show("读取通讯录权限成功 封装成功");
                    }
                });

                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
            grantResults) {
        Permissions4M.onRequestPermissionsResult(mActivity, requestCode, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


//    ====================================================================
//    @PermissionsGranted(CALL_LOG_CODE)// 通过记录权限授权成功的回调
//    public void storageAndCallGranted() {
//        ToastUtil.show("读取通话记录权限授权成功 in activity with annotation");
//    }
//
//    @PermissionsDenied(CALL_LOG_CODE)// 通话记录权限授权失败的回调
//    public void storageAndCallDenied() {
//        ToastUtil.show("读取通话记录权限授权失败 in activity with annotation");
//    }
//
//    @PermissionsRationale(CALL_LOG_CODE)//
//    public void storageAndCallNonRationale() {
//        ToastUtil.show("请开启读取通话记录权限授权 in activity with annotation");
//    }
//
//    //===================================================================
//    @PermissionsNonRationale({ CALL_LOG_CODE})
//    public void storageAndCallRationale(int code, final Intent intent) {
//        switch (code) {
//
//            case CALL_LOG_CODE:
//                new AlertDialog.Builder(mActivity)
//                        .setMessage("用户您好，我们需要您开启读取通话记录权限\n请点击前往设置页面\n(in activity with listener)")
//                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialog, int which) {
//                                startActivity(intent);
//                            }
//                        })
//                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialog, int which) {
//                                dialog.cancel();
//                            }
//                        })
//                        .show();
//                break;
//            default:
//                break;
//        }
//    }
}
